//app.js

var serviceUrl = "xxxx";//后台服务地址
var project = "xxxxx";//后台服务的项目名字

App({
  data: {
    userid:"",
    serviceUrl: serviceUrl,
    url:serviceUrl + project +"/",
    loadingHide:false,
    winHeight:wx.getSystemInfoSync().windowHeight
  },
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null
  },
  //发送 Post请求
  sendPost: function (url, dataForm, callback) {

    var that = this;
    wx.request({
      url: url,
      data: dataForm,
      method: "POST",
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', },
      success: function (res) {
        console.log(res)
        var data = res.data;
        callback(data);
      }
    })
  },
  //处理成功返回结果
  dealSuccess: function(res,showError){

    if (!res) {
      wx.showModal({
        title: '提示',
        content: '网络异常,请稍后再试',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    } else if (showError && res.data.result != "100"){
      console.log(res);
      wx.showToast({
        title: res.data.message,
        duration: 1000
      })
      return res.data;
    }else{
      return res.data;
    }
  },
  //获取用户
  getUserId: function(code,callback){
    var that = this;
    var url = this.data.url + "/getUserId.do";
    this.sendPost(url,{"code":code},function(res){
      console.log(res);
      that.data.userid = res.data;
      that.data.loadingHide = true;
      callback();
    });
  },
  //存储用户信息到后台
  storeUserInfo: function () {
    var that = this;
 
    if (that.data.userid && that.globalData.userInfo){
      var url = this.data.url + "/storeUserInfo.do";
      var dataForm = {
        "userId": that.data.userid,
        "nickName": that.globalData.userInfo.nickName,
        "avatarUrl": that.globalData.userInfo.avatarUrl,
        "city": that.globalData.userInfo.city,
        "country": that.globalData.userInfo.country,
        "gender": that.globalData.userInfo.gender,
        "language": that.globalData.userInfo.language,
        "province": that.globalData.userInfo.province
      };
      this.sendPost(url, dataForm, function (res) {
        console.log(res);
        
      });
    }
  },
  //作用：用于跳转到授权界面
  toSetAuth: function(desc){
    desc = "领取红包需要用户信息授权和语音授权,是否跳转到设置授权界面？";
    //
    var that = this;
    //1.弹框询问用户是否跳转到设置界面

    wx.showModal({
      title: '提示',
      content: desc,
      success: function (res) {
        if (res.confirm) {
          wx.openSetting({
            success: function (data) {
              if (data) {
                console.log(data);
                //用户信息授权
                if (data.authSetting["scope.userInfo"] == true) {
                  wx.getUserInfo({
                    success: res => {
                      //存储用户信息到全局变量
                      that.globalData.userInfo = res.userInfo
                      //存储用户信息
                      that.storeUserInfo();
                    }
                  })
                }
                //语音授权record
                if (data.authSetting["scope.record"] == true) {
                  wx.getUserInfo({
                    success: res => {
                      //不做处理
                    }
                  })
                }
              }
            },
            fail: function () {
              console.info("设置失败返回数据");
            }
          });


        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });

  },
  showMessage: function(msg){
    wx.showModal({
      title: '提示',
      content: msg,
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  }


})